import {
  CallHandler,
  ExecutionContext,
  Injectable,
  NestInterceptor
} from '@nestjs/common';
import { Observable, map } from 'rxjs';
import { responseSucceess } from '@/utils/commonResponse';
import { isArray, isObject } from 'class-validator';

interface Data<T> {
  data: T;
}

@Injectable()
export class SuccessResponse<T> implements NestInterceptor<T> {
  intercept(context: ExecutionContext, next: CallHandler): Observable<Data<T>> {
    return next.handle().pipe(
      //data即为 Service层或者Controller层的返回值
      map((data) => {
        const request = context.switchToHttp().getRequest();
        if (request.method === 'POST') {
          context.switchToHttp().getResponse().status(200);
        }
        return responseSucceess(data);
      })
    );
  }
}
